<template>
	<view>
		<!-- <u-search
			v-model="keyword"
			:show-action="false"
			@change="searchlist"
			placeholder="请输入关键字"	
		></u-search> -->
		<u-button type="primary" @click="toadd">添加设备</u-button>
		<view style="display: flex;">
			<u-button type="success" @click="reset">重置</u-button>
			<u-button type="warning" @click="pickerShow = true">通过机柜编号查询</u-button>
		</view>
		<u-picker :show="pickerShow" :columns="columns" :closeOnClickOverlay="true" @close="pickerShow = false"
			@cancel="pickerShow = false" @confirm="searchlist" ref="uPicker" :immediateChange="true"></u-picker>
		<u-list @scrolltolower="scrolltolower" v-if="lineequlist != false || status == 'loading'">
			<u-list-item v-for="(item, index) in lineequlist" :key="index">
				<u-cell :title="(index+1)+'、'+ (item.Name ? item.Name : '空')" 
				:label="'类型:' + (item.Type || '') + ' 型号:' + (item.Model || '') + ' 起始U:' + (item.U || '') + ' 结束U:' + (item.U2 || '')" isLink
					@click="openpage(item.Id)">
					<view slot="right-icon">
						<u-button type="primary" @click.native.stop="toconnect(item)" >连接详情</u-button>
					</view>
				</u-cell>
			</u-list-item>
			<u-loadmore :status="status" v-if="loading" />
		</u-list>
		<u-empty mode="data" icon="https://project.gxrunxiang.com/cdn/list.png" v-else>
		</u-empty>
	</view>
</template>

<script>
	import {
		getlineequlist,
		GetAddrByLatlng,
		LineequShelfNos
	} from '@/common/api.js'
	import coordtransform from 'coordtransform'
	export default {
		data() {
			return {
				lineequlist: [],
				keyword: "",
				pageIndex: 1, //当前页数
				pageSize: 20, //页数大小
				totalPage: "", //总页数
				total: 0, //数据总数
				status: 'loadmore', //加载状态
				loading: true,
				user: {},
				shelfNo: "",
				isFront: "",
				userfilter: {},
				NodeId: "",
				Addr: "",
				pickerShow: false,
				columns: []
			}
		},
		onLoad(option) {
			this.user = JSON.parse(uni.getStorageSync("$userinfo"))
			this.NodeId = option.NodeId
			var gcj01 = coordtransform.wgs84togcj02(option.Lng, option.Lat)
			GetAddrByLatlng(gcj01[1] + "," + gcj01[0]).then(res => {
				this.Addr = res;
			})		
		},
		onShow() {
			this.lineequlist = []
			this.getlist()
			LineequShelfNos(this.NodeId).then(res => {
				this.columns[0] = res
				this.columns[1] = ['前', '后']
			})
		},
		methods: {
			openpage(e) {
				uni.navigateTo({
					// url: "lineequdetail?Id=" + e 
					url: `lineequdetail?NodeId=${this.NodeId}&Id=${e}`
				})
			},
			async getlist() {
				this.status = 'loading'
				// let querytype = this.$route.query.querytype
				let para = {
					Parameters: [],
				}
				if (this.shelfNo != "") {
					// this.userfilter = {
					// 	"ConditionalList": [{
					// 		"Key": 0,
					// 		"Value": {
					// 			"FieldName": "Name",
					// 			"FieldValue": this.keyword,
					// 			"ConditionalType": "Like"
					// 		}
					// 	}, {
					// 		"Key": 1,
					// 		"Value": {
					// 			"FieldName": "Type",
					// 			"FieldValue": this.keyword,
					// 			"ConditionalType": "Like"
					// 		}
					// 	}]
					// }
					// para.jsonPara=JSON.stringify( [this.userfilter])
					para.Parameters.push({
						FieldName: "ShelfNo",
						ConditionalType: "Equal",
						FieldValue: this.shelfNo
					},{
						FieldName: "IsFront",
						ConditionalType: "Equal",
						FieldValue: this.isFront
					})
					para.OrderBys = ['U desc']
				} else {
					para.OrderBys = ['CreateTime desc']
				}
				para.Parameters.push({
					FieldName: "NodeId",
					ConditionalType: "Equal",
					FieldValue: this.NodeId
				})
				para.pageSize = this.pageSize
				para.pageIndex = this.pageIndex
				// if (querytype == "日志") {
				// }
				await getlineequlist(para).then(res => {
					let data = res.data
					this.lineequlist.push(...data)
					this.total = res.count
					this.totalPage = Math.ceil(this.total / this.pageSize)
				})
				if (this.lineequlist.length < this.pageSize) {
					this.loading = false; //如果数据列表小于当前页数，就让加载标签消失
					this.status = 'nomore';
				} else {
					this.status = 'loadmore';
				}
			},
			scrolltolower() {
				// 如果当前页数大于等于总页数，状态修改为没有更多了，不再继续往下执行代码
				if (this.pageIndex >= this.totalPage) {
					this.status = 'nomore';
					return;
				};
				this.status = 'loading'; //状态改为加载中
				this.pageIndex = ++this.pageIndex; //页面新增一页
				this.getlist(); //修改页数后，重新获取数据
			},
			async searchlist(e) {
				const {
					columnIndex,
					index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = this.$refs.uPicker
				} = e	
				this.lineequlist = [] //初始化
				this.pageIndex = 1 //初始化当前页数为1
				this.loading = true
				this.shelfNo = e.value[0]
				this.isFront = e.value[1]
				await this.getlist()
				this.pickerShow = false
			},
			toadd() {
				uni.redirectTo({
					url: `/pages/index/addlineequ?NodeId=${this.NodeId}&Addr=${this.Addr}`
				})
			},
			toconnect(e){
				uni.navigateTo({
					url: `lineconnect?nodeId=${e.NodeId}&equId=${e.Id}&equType=${e.Type}`
				})
			},
			async reset(){
				this.shelfNo = '';
				this.isFront = '';
				this.pageIndex = 1 //初始化当前页数为1
				this.loading = true
				this.lineequlist = [];
				await this.getlist()
			}
		},
	}
</script>

<style>

</style>